import {
  VxeColumnPropTypes,
  VxeFormItemProps,
  VxeGridInstance,
  VxeGridPropTypes,
  VxeTableDefines,
  VxeTableProps,
  VxeTablePropTypes
} from 'vxe-table'
import VxeUI from "vxe-pc-ui";
import XEUtils from 'xe-utils'
import {Ref} from "vue";
import {projectListApi} from "#/api/project/projectApi";
export const vxeTableFormSchema:  VxeGridPropTypes.FormConfig= {
  items: [
    { field: 'projectName', title: '项目名称', itemRender: { name: 'VxeInput',defaultValue:'' }, },
    { field: 'projectCode', title: '项目编码', itemRender: { name: 'VxeInput' } },
    { field: 'projectStatus', title: '项目状态', itemRender: { name: 'VxeInput' } },
    {
      itemRender: {
        name: 'VxeButtonGroup',
        options: [
          { type: 'submit', content: '搜索', status: 'primary' },
          { type: 'reset', content: '重置' }
        ]
      }
    }
  ]
}
/**
 * fixed  将列固定在左侧或者右侧
 * align  列对齐方式     left（左对齐）, center（居中对齐）, right（右对齐）
 * sortable 是否允许列排序
 */
export const vxeTableColumns: VxeGridPropTypes.Columns = [
  {type: 'checkbox',  width: '3%', fixed: 'left'},
  {type: 'seq', width: '4%', fixed: 'left'},
  {field: 'projectCode', title: '项目编码',  width: '8%', fixed: 'left',align: 'left', cellRender: {name: 'MyTableCellLink'}},
  {field: 'projectName', title: '项目名称', width: '10%', },
  {
    field: 'projectStatus', title: '项目状态',  width: '8%',sortable: true,
    filters: [
      {label: '启动', value: "enable"},
      {label: '禁用', value: "forbidden"},
      {label: '暂停', value: "pause"}
    ],
  },
  {field: 'appId', title: '应用id', width: '8%',},
  {field: 'projectLeader', title: '项目负责人', width: '8%',},
  {field: 'mailMonitor', title: '告警邮箱', width: '10%',},
  {field: 'description', title: '项目描述',  align:"left", showOverflow: true},
  {field: 'createTime', title: '创建时间', width: '8%',align:"left",sortable: true,},
  {field: 'updateTime', title: '修改时间', width: '8%',align:"left",sortable: true,},
  {
    title: '操作', width: 150, fixed: 'right', align: 'center',
    cellRender: {
      name: 'VxeButtonGroup',
      props: {
        mode: 'text'
      },
      options: [
        {content: '删除', status: 'error', name: 'del'}
      ],
      events: {
        click(cellParams, params) {
          VxeUI.modal.message({
            content: `点击了 ${params.name}`,
            status: 'success'
          })
        }
      }
    }
  },
];

// 模拟后台接口
export const findPageListApi = (page: any, sort: any, sorts: any, filters: any, form: any) => {
  return projectListApi(page);
}

// 模拟接口
export const deleteApi = (removeRecords: object[]) => {
  return new Promise<void>(resolve => {
    setTimeout(() => {
      console.log(`已删除 ${removeRecords.length} 行`, removeRecords)
      resolve()
    }, 150)
  })
}

// 模拟接口
export const saveApi = async (gridRef: Ref<VxeGridInstance<object> | undefined>, pendingRecords: object[], insertRecords: object[], updateRecords: object[]) => {
  console.log(123)
  const $grid = gridRef.value
  if ($grid) {
    const errMap = await $grid.validate(true)
    if (errMap) {
      VxeUI.modal.message({status: 'error', content: '校验不通过！'})
    } else {
      VxeUI.modal.message({status: 'success', content: '校验成功！'})
    }
  }
  return new Promise<void>(resolve => {
    setTimeout(() => {
      console.log(`删除行 ${pendingRecords.length} 行`, pendingRecords)
      console.log(`新增行 ${insertRecords.length} 行`, insertRecords)
      console.log(`更新行 ${updateRecords.length} 行`, updateRecords)
      resolve()
    }, 200)
  })
}






// 模拟保存接口
export const saveCustomSetting = (storeData: VxeTableDefines.CustomStoreData) => {
  return new Promise<void>(resolve => {
    setTimeout(() => {
      console.log("saveCustomSetting"+storeData)
      VxeUI.modal.message({
        status: 'success',
        content: '保存成功'
      })
      resolve()
    }, 200)
  })
}
// 模拟查询接口
export const findCustomSetting = (id: string) => {
  return new Promise<VxeTableDefines.CustomStoreData>(resolve => {
    setTimeout(() => {
      try {
        if (sessionStorage.getItem(id)) {
          resolve(JSON.parse(sessionStorage.getItem(id) || '') as VxeTableDefines.CustomStoreData)
          console.log("findCustomSetting"+id)
          VxeUI.modal.message({
            status: 'success',
            content: '异步还原用户个性化数据成功'
          })
        } else {
          resolve({})
        }
      } catch (e) {
        resolve({})
      }
    }, 500)
  })
}
